﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DLM.DependencyResolve;


namespace DLM.Web.MVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : BaseHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
               
                new[] { "DLM.Web.MVC.Controllers" }
                // Parameter defaults
            );

            routes.MapRoute(
             "ViewParty", // Route name
             "{controller}/{action}/{code}", // URL with parameters
             new { controller = "Account", action = "Activation", code = UrlParameter.Optional }
             );

           // routes.MapRoute(
           //"SearchUser", // Route name
           //"{controller}/{action}/{type}/{para}", // URL with parameters
           //new { controller = "User", action = "SearchUser", type = UrlParameter.Optional,para= UrlParameter.Optional }
           //); 


        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            // Initial IoC setting
            InitializeFactory();
            // StackExchange.Profiling.MiniProfilerEF.Initialize(); 

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication currentApp = (HttpApplication)sender;
            // Get current http request's httpcontext object
            HttpContext currentContext = currentApp.Context;
            if (currentContext.User != null)
            {
                // Verify user
                if (currentContext.Request.IsAuthenticated == true)
                {
                    System.Web.Security.FormsIdentity fi = (System.Web.Security.FormsIdentity)currentContext.User.Identity;
                    // Get authenticate ticket
                    System.Web.Security.FormsAuthenticationTicket ticket = fi.Ticket;
                    // Get user data
                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    currentContext.User = new System.Security.Principal.GenericPrincipal(fi, roles);
                }
            }
        }
    }
}